﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.IO;

namespace Ui
{
    public partial class Form3 : UIForm
    {
        public Form3()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName.Trim();
                string[] gs = textBox2.Text.Split('.');
                if (gs[1] == "adb")
                {
                    radioButton1.Checked = true;
                    label3.Text = gs[1];
                }
                else
                {
                    radioButton2.Checked = true;
                    label3.Text = gs[1];
                }
                uiButton1.Enabled = true;
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                saveFileDialog1.Filter = "csv|*.csv";
            }
            else if (radioButton2.Checked)
            {
                saveFileDialog1.Filter = "adb|*.adb";
            }
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = saveFileDialog1.FileName.Trim();
                uiButton2.Enabled = true;
                uiButton1.Enabled = false;
            }
                
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                File.Create(textBox1.Text).Close();
                List<string> list = new List<string>(File.ReadAllLines(textBox2.Text.Trim()));
                foreach (string line in list)
                {
                    string str = null;
                    if (line.Substring(0, 1) == "#")
                    {
                        string[] vs = line.Substring(1, line.Length - 2).Split('!');
                        foreach (var item in vs)
                        {
                            string[] vs1 = item.Split('@');
                            str+= vs1[0]+"("+vs1[1]+"),";
                        }
                        write_file(str);
                    }
                    else if (line.Substring(0, 1) == "$")
                    {
                        //string[] vs = line.Substring(1, line.Length - 2).Split('%');
                        string s = line.Substring(1, line.Length - 2).Replace('%', ',');
                        Console.WriteLine(s);
                        write_file(s);
                    }
                }
            }
            else
            {
                File.Create(textBox1.Text).Close();
                List<string> list = new List<string>(File.ReadAllLines(textBox2.Text.Trim()));
                string str = null;
                write_file("#"+list[0].Replace(',','!'));
                int i = 0;
                foreach (string line in list)
                {
                    if(i>0)
                        write_file("$" + line.Replace(',', '%'));
                    i++;
                }
             }
        }
        public void write_file(string s)
        {
            File.AppendAllLines(textBox1.Text, new string[] { s });
        }
        private void Form3_Load(object sender, EventArgs e)
        {
            uiButton2.Enabled = false;
            uiButton1.Enabled = false;
        }
    }
}
